<template>
  <a-modal
    :title="title"
    :width="1000"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          :hidden="true"
          label="id"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input
            v-decorator="['id']"
          />
        </a-form-item>
        <a-row :getter="24">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="合同订单编号"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                placeholder="合同订单编号"
                :disabled="editable || showable"
                v-decorator="['orderCode']"/>
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="业务类型"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                placeholder="业务类型"
                :disabled="editable || showable"
                v-decorator="['businessType',{rules: [{required: true, message: '选择业务类型'}]}]"/>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :getter="24">
          <a-col :md="12" :sm="24">
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="备注"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                placeholder="备注"
                :disabled="editable || showable"
                v-decorator="['remark']"/>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :getter="24">
          <vxe-toolbar>
            <template v-slot:buttons>
              <a-button @click="$refs.editMatForm.init('add')" icon="plus" type="primary" v-if='type !== "show"'>新增物料</a-button>
            </template>
          </vxe-toolbar>
          <vxe-table
            border
            resizable
            show-overflow
            highlight-hover-row
            ref="xTable"
            height="500"
            :data="tableData"
            :loading='tableLoading'
          >
            <vxe-column type="seq" width="60"></vxe-column>
            <vxe-column field="matCode" title="物料编码"></vxe-column>
            <vxe-column field="matName" title="物料名称"></vxe-column>
            <vxe-column field="matBarcode" title="物料条码"></vxe-column>
            <vxe-column field="supplier" title="供应商编码"></vxe-column>
            <vxe-column field="num" title="数量"></vxe-column>
            <vxe-column title="操作" width="300" v-if='type !== "show"'>
              <template slot-scope="scope">
                <a-button @click="editEvent(scope.row)">编辑</a-button>
                <a-button @click="removeEvent(scope.row)">删除</a-button>
              </template>
            </vxe-column>
          </vxe-table>
        </a-row>
      </a-form>
    </a-spin>
    <edit-mat-form @ok='handleMatok' ref='editMatForm'></edit-mat-form>
  </a-modal>
</template>

<script>
import { FormMixin } from '@/mixins/FormMixin'
import { info, saveInputOrder } from '@/api/side/warehouseOrder'
import editMatForm from '@/views/main/side/warehouseOrder/editMatForm'
import pick from 'lodash.pick'

export default {
  name: 'OrderForm',
  mixins: [FormMixin],
  components: {
    editMatForm
  },
  data() {
    return {
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this),
      tableData: [],
      selectRow: null,
      tableLoading: false,
      type: 'add'
    }
  },
  methods : {
    edit (code, type) {
      this.visible = true
      this.type = type
      const { form: { setFieldsValue, resetFields } } = this
      resetFields()
      this.tableData = []
      this.selectRow = null
      if(["show"].includes(type)){
        this.confirmLoading = true
        info(code).then(res => {
          this.tableData = res.data.items
          const record = res.data
          this.$nextTick(() => {
            setFieldsValue(pick(record, ['orderCode', 'businessType', 'remark','type']))
          })
        }).catch(err => {
          this.$message.error(err.msg)
        }).finally(() => {
          this.confirmLoading = false
        })
      }
    },
    // 结束保存用户信息
    handleOk () {
      if(this.type === 'show'){
        this.handleCancel()
        return;
      }
      this.form.validateFields((err, values) => {
        if (!err) {
          this.confirmLoading = true
          values.items = this.$refs.xTable.tableData.map(item => {
            return {
              matCode: item.matCode,
              matName: item.matName,
              matBarcode: item.matBarcode,
              supplier: item.supplier,
              num: item.num
            }
          })
          saveInputOrder(values).then(res => {
            this.confirmLoading = false
            this.handleCancel()
            this.$emit('ok')
          }).catch(err => {
            this.$message.error(err.msg)
            this.confirmLoading = false
          })
        }
      })
    },
    editEvent(row){
      this.selectRow = row
      this.$refs.editMatForm.init('edit',row)
    },
    async removeEvent (row) {
      const type = await this.$XModal.confirm('您确定要删除该数据?')
      const $table = this.$refs.xTable
      if (type === 'confirm') {
        $table.remove(row)
      }
    },
    handleMatok(row){
      this.tableLoading = true
      setTimeout(() => {
        const $table = this.$refs.xTable
        if (this.selectRow) {
          Object.assign(this.selectRow, row)
        } else {
          $table.insert(row)
        }
        this.tableLoading = false
      }, 500)
    }
  }
}
</script>

<style scoped>

</style>